<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/style.css">
    <style>
        .flex-box {
            display: flex;
            align-items: flex-start;
        }
        .left-panel {
            border: 2px solid;
            margin-right: 20px;
            padding: 0 20px;
            min-width: 400px;
        }
        pre {
            white-space: pre-line;
            padding-left: 2em;
        }
        .container {
            flex: 1 0 auto;
        }
    </style>
</head>
<body>
    <h3>grid-template-columns 和 grid-template-rows</h3>
    <div class="flex-box">
        <div class="left-panel">
            <p><label for=""><input type="radio" name="template" value="simple">简单例子</label>
                <pre>
                    .container {
                        grid-template-columns: 100px 100px 100px;
                        grid-template-rows: 80px 100px 50px;
                    }
                </pre>
            </p>
            <p><label for=""><input type="radio" name="template" value="repeat">repeat()</label>
                <pre>
                    .container {
                        grid-template-columns: repeat(3, 33.33%);
                        grid-template-rows: repeat(2, 100px 20px 80px);
                    }
                </pre>
            </p>
            <p><label for=""><input type="radio" name="template" value="auto">auto-fill关键字</label>
                <pre>
                    .container {
                        grid-template-columns: repeat(auto-fill, 100px);
                    }
                </pre>
            </p>
            <p><label for=""><input type="radio" name="template" value="fr">fr关键字</label>
                <pre>
                    .container {
                        grid-template-columns: 1fr 1fr 1fr;
                    }
                </pre>
            </p>
        </div>
        <div class="container" id="gridBox">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
            <div class="item">6</div>
            <div class="item">7</div>
            <div class="item">8</div>
            <div class="item">9</div>
            <div class="item">10</div>
            <div class="item">11</div>
            <div class="item">12</div>
        </div>
    </div>
    <script>
        let inputs = document.querySelectorAll('input[name=template]');
        let box = document.getElementById('gridBox');
        Array.from(inputs).forEach(ele => {
            ele.onchange = function() {
                let value = this.value;
                let columns = '';
                let rows = '';
                switch (value) {
                    case 'simple':
                        columns = '100px 100px 100px';
                        rows = '80px 100px 50px';
                        break;
                    case 'repeat':
                        columns = 'repeat(3, 33.33%)';
                        rows = 'repeat(2, 100px 20px 80px)';
                        break;
                    case 'auto':
                        columns = 'repeat(auto-fill, 100px)';
                        break;
                    case 'fr':
                        columns = '1fr 1fr 1fr';
                        break;
                } 
                box.style.gridTemplateColumns = columns;
                box.style.gridTemplateRows = rows;
            }
        })
    </script>
</body>
</html>